﻿using System;
using System.Collections.Generic;
using System.Text;

using SourceGrid;

namespace iPdf
{
    partial class TataGridBase<T>: SourceGrid.Grid
    {
        #region Fields
        protected System.Drawing.Font gridFont = new System.Drawing.Font("Arial", 9);
        protected System.Drawing.Font editorFont = new System.Drawing.Font("Arial", 13);
        protected int gridRowHeight = 28;
        #endregion

        #region Properties
        public int[] SelectedRowsIndex
        {
            get
            {
                return Selection.GetSelectionRegion().GetRowsIndex();
            }
        }

        public bool SelectionExists
        {
            get
            {
                return SelectedRowsIndex.Length > 0;
            }
        }

        public bool AllSelected
        {
            get
            {
                return SelectedRowsIndex.Length == RowsCount - 1;
            }
        }

        public bool HasItems
        {
            get
            {
                return RowsCount > 1;
            }
        }

        public bool SingleSelected
        {
            get
            {
                return SelectedRowsIndex.Length == 1;
            }
        }

        public int SingleSelectedRowId
        {
            get
            {
                if (SingleSelected)
                {
                    return SelectedRowsIndex[0];
                }
                else
                {
                    return this.RowsCount == 2 ? 1 : -1;
                }
            }
        }

        #endregion

        #region Ctor

        protected TataGridBase()
        {
            InitializeColumns();
        }

        #endregion

        #region Movers

        public delegate bool RowJudge(int row, int removedRowsCnt, int rowsCnt);
        public delegate void RowRemover(int row);

        public void RemoveRows(RowJudge judge, RowRemover remover)
        {
            int rowsCnt = RowsCount;
            int removedRowsCnt = 0;
            for (int i = 1; i < rowsCnt; ++i)
            {
                int adjustedRowId = i - removedRowsCnt;
                if (judge(i, removedRowsCnt, rowsCnt))
                {
                    remover(adjustedRowId);
                    ++removedRowsCnt;
                }
            }
        }

        public virtual void RemoveSelection()
        {
            if (!SelectionExists)
            {
                return;
            }

            int[] rowsIndex = SelectedRowsIndex;

            int removedRowsCnt = 0;
            int adjustedRowIndex = 0;
            foreach (int rowIndex in rowsIndex)
            {
                adjustedRowIndex = rowIndex - removedRowsCnt;
                Rows.Remove(adjustedRowIndex);
                ++removedRowsCnt;
            }

            if (RowsCount > 1)
            {
                if (adjustedRowIndex <= RowsCount - 1)
                {
                    Selection.SelectRow(adjustedRowIndex, true);
                }
                else
                {
                    Selection.SelectRow(adjustedRowIndex - 1, true);
                }
            }
        }

        public void InvertRemoveRow(int rowPos)
        {
            if (rowPos >= 1 && rowPos < RowsCount)
            {
                if (rowPos > 1)
                {
                    Rows.Move(rowPos, 1);
                }

                Redim(2, ColumnsCount);
                Selection.SelectRow(1, true);
            }
        }

        public void InvertRemoveSelection()
        {
            List<int> toRemove = new List<int>();
            for (int i = 1; i < RowsCount; ++i)
            {
                if (!Selection.IsSelectedRow(i))
                {
                    toRemove.Add(i);
                }
            }

            int removedCount = 0;
            foreach (int i in toRemove)
            {
                int adjustedIndex = i - removedCount;
                Rows.Remove(adjustedIndex);
                ++removedCount;
            }

            SelectAll();
        }

        protected void SelectAll()
        {
            SourceGrid.Position start = new SourceGrid.Position(1, 1);
            SourceGrid.Position end = new SourceGrid.Position(RowsCount - 1, 1);
            SourceGrid.Range allRange = new SourceGrid.Range(start, end);
            Selection.SelectRange(allRange, true);
        }

        public virtual void RemoveAll()
        {
            Redim(1, ColumnsCount);
        }


        #endregion
    }
}
